<template>
  <a-modal
    title="考核评定"
    :width="1200"
    :visible="visible"
    :confirmLoading="loading"
    @ok="() => ok()"
    @cancel="() => { $emit('cancel') }">
    <div style="display: flex;">
      <div style="wdith: 800px">
        <ProdBonus
          title="规则配置"
          ref="bonus"
          :count="count"
          :edit="edit"
          :rates="rates"
          :marks="marks" />
      </div>
      <div style="flex-direction: column;display: flex">
        <a-card-grid
          style="width: 150px; text-align: center;height: 80px;margin-top: 57px;"
          :hoverable="false">实际完成情况(0~1)</a-card-grid>
        <a-card-grid
          style="width: 150px; text-align: center;height: 80px;"
          :hoverable="false">
          <a-input-number :min="0" :max="1" :precision="2" v-model="p1" />
        </a-card-grid>
        <a-card-grid
          style="width: 150px; text-align: center;height: 80px;"
          :hoverable="false">
          <a-input-number :min="0" :max="1" :precision="2" v-model="p2" />
        </a-card-grid>
        <a-card-grid
          style="width: 150px; text-align: center;height: 80px;"
          :hoverable="false">
          <a-input-number :min="0" :max="1" :precision="2" v-model="p3" />
        </a-card-grid>
        <a-card-grid
          v-if="count === 4"
          style="width: 150px; text-align: center;height: 80px;"
          :hoverable="false">
          <a-input-number v-model="p4" />
        </a-card-grid>
      </div>
    </div>
  </a-modal>
</template>
<script>
import ProdBonus from '@/views/regulation/tables/compontents/ProdBonus'
import { getPerformanceConfigInfo } from '@/api/performanceConfig'
import { message } from 'ant-design-vue'
export default {
  name: 'EvaluateForm',
  components: {
    ProdBonus
  },
  props: {
    isEdit: {
      type: Boolean,
      default: () => false
    },
    visible: {
      type: Boolean,
      default: () => false
    },
    year: {
      type: [Number, String],
      default: undefined
    },
    version: {
      type: [Number, String],
      default: undefined
    },
    rule: {
      type: Object,
      default: undefined
    }
  },
  watch: {
    visible: function (v) {
      if (v) {
        this.p1 = this.rule.evaluation ? this.rule.evaluation.evaluation1 : undefined
        this.p2 = this.rule.evaluation ? this.rule.evaluation.evaluation2 : undefined
        this.p3 = this.rule.evaluation ? this.rule.evaluation.evaluation3 : undefined
        this.p4 = this.rule.evaluation ? this.rule.evaluation.evaluation4 : undefined
        const job2 = this.rule.job2
        const year = this.year
        const version = this.version
        let job1 = ''
        // 如果是 job2 === 3项目经理，job1 = 2
        // 否则 为 3，生产提成规则

        if (job2 === 3) {
          job1 = 2
          this.count = 4
          this.rates = [undefined, undefined, undefined, undefined]
          this.marks = [
            '完成项目立项时设定的毛利目标',
            '完成项目立项时设定的工期目标',
            '完成项目立项时设定的质量目标',
            '完成项目的其它要求根据《项目审查表》完成情况'
          ]
        } else {
          job1 = 3
          this.count = 3
          this.rates = [undefined, undefined, undefined]
          this.marks = [
            '完成项目立项时设定的工期目标',
            '完成项目立项时设定的质量目标',
            '完成项目的其它要求根据《项目审查表》完成情况'
          ]
        }
        if (year && version && job2 && job1) {
          getPerformanceConfigInfo({ year, version, job2, job1 }).then(res => {
            if (job2 === 3) {
              const { ext9, ext10, ext11, ext12 } = res.data || {}
              this.rates = [ext9, ext10, ext11, ext12]
            } else {
              const { ext1, ext2, ext3 } = res.data || {}
              this.rates = [ext1, ext2, ext3]
            }
          })
        }
      }
    }
  },
  data () {
    return {
      count: 4,
      edit: false,
      rates: [undefined, undefined, undefined, undefined],
      marks: ['', '', '', ''],
      loading: false,
      p1: undefined,
      p2: undefined,
      p3: undefined,
      p4: undefined
    }
  },
  methods: {
    ok () {
      let min = 2
      let max = -1
      let hasEmpty = false
      if (this.count === 4) {
        min = Math.min(this.p1, this.p2, this.p3, this.p4)
        max = Math.max(this.p1, this.p2, this.p3, this.p4)
        hasEmpty = [this.p1, this.p2, this.p3, this.p4].some(item => item === undefined || item === null)
      } else {
        min = Math.min(this.p1, this.p2, this.p3)
        max = Math.max(this.p1, this.p2, this.p3)
        hasEmpty = [this.p1, this.p2, this.p3].some(item => item === undefined || item === null)
      }
      if (min < 0 || max > 1 || hasEmpty) {
        message.error('参数填写不规则，确保所有参数填写完整，且属于0～1')
      } else {
        this.$emit('ok', {
          evaluation1: this.p1,
          evaluation2: this.p2,
          evaluation3: this.p3,
          evaluation4: this.p4
        })
      }
    }
  }
}
</script>
<style lang="less" scoped>
.cardWrap {
  width: calc(50% - 40px);
}
</style>
